const _Number = {
  value: undefined,
  onChange: function onChange(e, callback, precision, min) {
    var input = this.getValueFromEvent(e).trim();
    callback(this.toNumber(input)); // valid number or invalid string
  },
  getValueFromEvent: function getValueFromEvent(e) {
    return e.target.value;
  },
  // '1.' '1x' 'xx' ''  => are not complete numbers
  isNotCompleteNumber: function isNotCompleteNumber(num) {
    return (
      isNaN(num) ||
      num === '' ||
      num.toString().indexOf('.') === num.toString().length - 1
    )
  },
  toNumber: function toNumber(num) {
    if (this.isNotCompleteNumber(num)) {
      return Number(num);
    }
    return num;
  },

  toPrecisionAsStep: function toPrecisionAsStep(num) {
    if (this.isNotCompleteNumber(num) || num === '') {
      return num
    }
    var precision = Math.abs(this.getMaxPrecision(num))
    if (precision) {
      return Number(num).toFixed(precision)
    }
    return num.toString()
  },
}

export default _Number
